<?php

declare(strict_types=1);

namespace App\Overwrites;

use App\Constants\Lang;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\TranslatorInterface;
use Mine\Annotation\DependProxy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

#[DependProxy(values: [ \Hyperf\Validation\Middleware\ValidationMiddleware::class ])]
class ValidationMiddleware extends \Hyperf\Validation\Middleware\ValidationMiddleware
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        if (is_api()) {
            // 设置请求语言
            $lang = $request->getHeaderLine('X-Lang') ?: Lang::ZH_CN;
            if (in_array($lang, Lang::$langs, true)) {
                ApplicationContext::getContainer()->get(TranslatorInterface::class)->setLocale($lang);
            }
        }

        return parent::process($request, $handler);
    }
}
